<template>
  <div>
    <el-tree
      ref="tree"
      show-checkbox
      default-expand-all
      check-strictly
      :data="list"
      :props="{label:'name'}"
      node-key="id"
    />
    <el-row type="flex" justify="end">
      <el-button type="warning" @click="HSubmit">确认提交</el-button>
      <el-button type="danger">取消</el-button>
    </el-row>
  </div>

</template>

<script>
import { getPermissionList } from '@/api/permission'
import { treeInfo } from '@/utils/treeinfo'
import { assignPerm, getRoleDetail } from '@/api/setting'
export default {

  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async HSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPerm({ id: this.curId, permIds: permIds })
      this.$message.success('分配成功')
      this.loadPermissionList()
    },
    async  loadPermissionList() {
      try {
        const res = await getPermissionList()
        const detail = await getRoleDetail(this.curId)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        this.list = treeInfo(res.data)
      } catch (error) {
        this.$message.error(error)
      }
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
